<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>案例_分类导航</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        a {
            text-decoration: none;
            color: #333;
        }

        ul {
            list-style: none;
        }

        .container {
            width: 980px;
            margin: 0 auto;
        }

        .top {
            display: flex;
            width: 100%;
            height: 80px;
            align-items: center;
            justify-content: space-around;
            position: relative;
        }

        .top>li {
            text-align: center;
        }

        .top>li>a {
            border-bottom: 1px solid transparent;
        }

        .top>li:hover>a {
            border-color: #27ba9b;
            color: #27ba9b;
        }

        .top>li:hover>.sub {
            opacity: 1;
            transform: scale(1, 1);
        }

        .sub {
            position: absolute;
            left: 0;
            top: 80px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            display: flex;
            align-items: center;
            height: 100px;
            opacity: 0;
            transform: scale(1, 0);
            transform-origin: top center;
            transition: all 0.3s .1s;
        }

        .sub li {
            width: 120px;
            text-align: center;
        }

        .sub li a {
            display: block;
            font-size: 14px;
            padding-top: 8px;
        }

        .sub li a span {
            display: block;
        }

        .sub li a img {
            width: 60px;
            height: 60px;
            vertical-align: middle;
        }
    </style>
</head>

<body>
    <div class="container">
        <ul class="top" id="top">
            <li>
                <a href="javascript:;">首页</a>
                <ul class="sub">
                    <li>
                        <a href="javascript:;">
                        <span>砂锅厨具</span>
                        <img src="https://yanxuan.nosdn.127.net/3102b963e7a3c74b9d2ae90e4380da65.png?quality=95&imageView" alt="">
                        </a>
                    </li>
                </ul>
            </li>
        </ul>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/axios@0.27.2/dist/axios.min.js"></script>
    <script>
        // 基地址
        axios.defaults.baseURL = 'http://ajax-api.itheima.net'

        // 拼接ul>li字符串
        function buildStr(res) {
            const str = res.map(item => {
                const obj = item.data.data
                console.log(obj)
                const lis = obj.children.map(o =>{
                    return `<li>
                                <a>
                                    <span>${o.name}</span>
                                    <img src="${o.picture}"/>
                                </a>
                        </li>`
                }).join('')

                return `<li>
                        <a href="">${obj.name}</a>
                        <ul class="sub">
                            ${lis}
                        </ul>
                    </li>`

            }).join('')
            return str
        }
        
        async function getMenu() {
            // 1. 拿到一级菜单
            const res = await axios.get('/api/category/top')
            const topMenus = res.data.data
            // 2. 拿所有的二级菜单
            //    2.1 创建一个promse数组：每一个元素都是一个ajax请求
            const ajaxPromise = topMenus.map(it => axios.get('/api/category/sub?id='+it.id))
            // Promise.all([promise1, promise2....])
            //    2.2 调用promise.all 
            const r = await Promise.all(ajaxPromise)  // ===> promise对象；状态， 值
            
            // console.log(r)
            // axios.get('/api/category/sub/'+topMenus[0].id)
            return r
        }
        
        async function createMenu(){
            try {
                // 获取菜单信息
                const res = await getMenu()
                // 拼接ul>li字符串
                const str = buildStr(res)
                console.log(str)
                // 设置字符串
                document.querySelector('#top').innerHTML = str

            } catch(err){
                console.log(err)
            }
        }

        createMenu()

    </script>
</body>

</html>